package com.zyao89.view.zloading.clock;

import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/19.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class ClockBuilder extends CircleBuilder {
    private float mOuterRadius;
    private float mCircleSpace;
    private Paint mStrokePaint;
    private Paint mFillPaint;
    private RectFloat mBtnRectF;
    private RectFloat mBottomBtnRectF;

    @Override
    protected void initParams(Context context, Color color) {
        super.initParams(context, color);
        initOptions(context);
        createStrokePaint(color);
        createFillPaint(color);
    }

    private void initOptions(Context context) {
        float allSize = getAllSize();
        mCircleSpace = 4;
        mOuterRadius = NumCalcUtil.subtract(allSize, mCircleSpace);
        float btnWidth = dip2px(context, 8);
        float btnHeight = dip2px(context, 3);
        float bottomBtnWidth = dip2px(context, 3);
        float bottomBtnHeight = dip2px(context, 2);
        mBtnRectF = new RectFloat(NumCalcUtil.subtract(getViewCenterX(), btnWidth / 2),
            NumCalcUtil.subtract(NumCalcUtil.subtract(NumCalcUtil.subtract(getViewCenterY(), allSize), bottomBtnHeight), btnHeight),
            NumCalcUtil.add(getViewCenterX(), btnWidth / 2),
            NumCalcUtil.subtract(NumCalcUtil.subtract(getViewCenterY(), allSize), bottomBtnHeight));
        mBottomBtnRectF = new RectFloat(NumCalcUtil.subtract(getViewCenterX(), bottomBtnWidth / 2),
                NumCalcUtil.subtract(NumCalcUtil.subtract(getViewCenterY(), allSize), bottomBtnHeight),
                NumCalcUtil.add(getViewCenterX(), bottomBtnWidth / 2),
            NumCalcUtil.subtract(getViewCenterY(), allSize));
    }

    private void createStrokePaint(Color color) {
        mStrokePaint = new Paint();
        mStrokePaint.setStyle(Paint.Style.STROKE_STYLE);
        mStrokePaint.setStrokeWidth(mCircleSpace);
        mStrokePaint.setColor(color);
    }

    private void createFillPaint(Color color) {
        mFillPaint = new Paint();
        mFillPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        mFillPaint.setColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 外圆
        canvas.drawCircle(getViewCenterX(), getViewCenterY(), mOuterRadius, mStrokePaint);

        // 上方按钮
        canvas.drawRect(mBtnRectF, mFillPaint);
        canvas.drawRect(mBottomBtnRectF, mFillPaint);

        // 右侧按钮
        canvas.save();
        canvas.rotate(45, getViewCenterX(), getViewCenterY());
        canvas.drawRect(mBottomBtnRectF, mFillPaint);
        canvas.restore();
        canvas.translate(0, 20);
    }

    @Override
    protected void setAlpha(int alpha) {
        super.setAlpha(alpha);
        mStrokePaint.setAlpha(alpha);
        mFillPaint.setAlpha(alpha);
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        super.setColorFilter(colorFilter);
        mStrokePaint.setColorFilter(colorFilter);
        mFillPaint.setColorFilter(colorFilter);
    }
}
